package com.zjdiepu.www.rest.api.third;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.ibiz.third.IJuheBrandService;
import com.zjdiepu.www.ibiz.third.IJuheCarService;
import com.zjdiepu.www.ibiz.third.IJuheSeriesService;
import com.zjdiepu.www.model.third.dto.JuheBrand;
import com.zjdiepu.www.model.third.dto.JuheCar;
import com.zjdiepu.www.model.third.dto.JuheSeries;
import com.zjdiepu.www.model.third.ext.JuheBrandExt;
import com.zjdiepu.www.model.third.ext.JuheCarExt;
import com.zjdiepu.www.model.third.ext.JuheSeriesExt;
import com.zjdiepu.www.model.third.request.CarModelRequest;
import com.zjdiepu.www.model.third.response.CarModelResponse;

/**
 * 车辆搜索Controller层<br/>
 * 
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 19时30分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value = "CarRestController", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = { "车辆搜索" }, produces = "application/json;charset=UTF-8")
public class CarRestController extends BaseController {
	@Resource
	private TokenUserHelper tokenUserHelper;
	@Resource
	private IJuheCarService juheCarService;
	@Resource
	private IJuheSeriesService juheSeriesService;
	@Resource
	private IJuheBrandService juheBrandService;

	/**
	 * @author xiongxiaotun <br/>
	 * @Title: carModelList <br/>
	 * @Description: 车辆搜索 <br/>
	 * @param carModelRequest
	 * @return
	 * @version V1.0 <br/>
	 */
	@ApiOperation(value = "车辆搜索", response = CarModelResponse.class, httpMethod = "POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue = "ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue = "Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String") })
	@RequestMapping("carModelList")
	public ApiResponse carModelList(
			@Validated @RequestBody CarModelRequest carModelRequest) {
		String nameCarModel = carModelRequest.getNameCarModel();
		if(StringUtils.isNotBlank(nameCarModel)) {
			carModelRequest.setNameCarModel(nameCarModel.replaceAll("@@", " "));
		}
		tokenUserHelper.getUser();
		JuheCarExt juheCarExt = new JuheCarExt();
		juheCarExt.setBaseWhere();
		juheCarExt.setCxname(carModelRequest.getNameCarModel().replaceAll("%20", " ")); //+为空格
		juheCarExt.setOrderSql("`CXNAME`");
		juheCarExt.setPage(carModelRequest.getPage());
		juheCarExt.setRows(carModelRequest.getRows());

		PageVo page = getPage(juheCarExt);
		int records = juheCarService.countByExt(juheCarExt);
		page.setRecords(records);
		if (records > 0) {
			List<JuheCar> juheCars = juheCarService.selectByExt(juheCarExt);
			List<CarModelResponse> carModelResponses = mapJuheCarModels(juheCars);
			page.setRows(carModelResponses);
		} else {
			page.setRows(CollectionInitUtil.emptyList());
		}
		return ApiResponse.success(page);
	}
	
	// ============================================辅助方法===========================================

	/**
	 * 转换属性：展示
	 * 
	 * @param juheCars
	 * @return
	 */
	private List<CarModelResponse> mapJuheCarModels(List<JuheCar> juheCars) {
		if (CollectionUtils.isEmpty(juheCars)) {
			return CollectionInitUtil.emptyList();
		}
		List<CarModelResponse> carModelResponses = CollectionInitUtil
				.emptyList();
		for (JuheCar juheCar : juheCars) {
			CarModelResponse carModelResponse = mapDpBaseCarModel(juheCar);
			if (carModelResponse != null) {
				carModelResponse.setPriceNewcar(juheCar.getPrice());//新车指导价格
				carModelResponses.add(carModelResponse);
			}
		}
		return carModelResponses;
	}

	/**
	 * 转换属性：展示
	 * @param juheCar
	 * @return
	 */
	private CarModelResponse mapDpBaseCarModel(JuheCar juheCar) {
		if (juheCar == null) {
			return null;
		}
		CarModelResponse carModelResponse = new CarModelResponse();
		carModelResponse.setCarModel(juheCar.getCarId());
		carModelResponse.setNameCarModel(juheCar.getCxname());
		String seriesId = juheCar.getSeriesId();
		if (StringUtils.isBlank(seriesId)) {
			return null;
		}
		JuheSeriesExt juheSeriesExt = new JuheSeriesExt();
		juheSeriesExt.setBaseWhere();
		juheSeriesExt.setXlid(seriesId);
		juheSeriesExt.setRows(1);

		List<JuheSeries> juheSeries = juheSeriesService
				.selectByExt(juheSeriesExt);
		if (CollectionUtils.isEmpty(juheSeries)) {
			return null;
		}
		JuheSeries juheSery = juheSeries.get(0);
		carModelResponse.setCarSeries(juheSery.getXlid());
		carModelResponse.setNameCarSeries(juheSery.getXlname());

		String brandId = juheSery.getBrandId();
		if (StringUtils.isBlank(brandId)) {
			return null;
		}

		JuheBrandExt juheBrandExt = new JuheBrandExt();
		juheBrandExt.setBaseWhere();
		juheBrandExt.setThirdId(brandId);
		juheBrandExt.setRows(1);

		List<JuheBrand> juheBrands = juheBrandService.selectByExt(juheBrandExt);
		if (CollectionUtils.isEmpty(juheBrands)) {
			return null;
		}
		JuheBrand juheBrand = juheBrands.get(0);
		carModelResponse.setCarBrand(juheBrand.getThirdId());
		carModelResponse.setNameCarBrand(juheBrand.getBigPpname());
		return carModelResponse;
	}
}
